package org.practice.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.practice.dao.DaoFactory;
import org.practice.dao.UserDao;
import org.practice.form.RegisterForm;
import org.practice.model.User;

public class RegisterOneAction extends Action {
	private static Logger logger = Logger.getLogger(RegisterOneAction.class);

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		RegisterForm rf = (RegisterForm) form;
		//ImageAction 设置session.setAttribute("number", number);
		String number = (String) session.getAttribute("number");
		String verifyNumber = rf.getTxtVerifyCode();
		if(verifyNumber!=null&&number.equals(verifyNumber)){
			User user = new User();
			BeanUtils.copyProperties(user, rf);
			UserDao userDao = DaoFactory.createUserDao();
			userDao.addUser(user);
			// send info to user-email
			logger.info("email:" + user.getVerifyCode() + "-" + user.getId());
			return mapping.findForward("success");
		}else {
			ActionMessages errors = new ActionMessages();
			errors.add("error.verify.number", new ActionMessage("error.verify.number"));
			saveErrors(request, errors);
			return mapping.findForward("fail");
		}
	}

}
